/*
 *  Copyright 2005 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.taglib.core.html;

import org.apache.struts.taglib.TagUtils;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.JspFragment;
import java.io.StringWriter;
import java.io.IOException;

import com.blandware.atleap.webapp.taglib.core.BaseHandlerTag;


/**
 * <p>Tag for 'submit' element. Originally taken from Struts SubmitTag.</p>
 * <p>
 * Following attributes are supported:
 * <ul>
 * <li>
 * <b>property</b> - name of the generated input field
 * </li>
 * <li>
 * <b>value</b> - value of the button label
 * </li>
 * <li>
 * <b>disableButtonOnSubmit</b> - whether to disable submit button after it was pressed
 * to prevent multiple submittion. This can be 'true' or 'false',
 * default is 'false'.
 * </li>
 * </ul>
 * Some attributes are inherited from {@link BaseHandlerTag}.
 * </p>
 * <p>
 * This tag can also have body, its content will be rendered as submit button
 * label. <b>Value</b> has precedence over body. If none of <b>value</b> and
 * tag body has been specified, button label will be 'Submit'.
 * </p>
 *
 * <p><a href="SubmitTag.java.html"><i>View source</i></a></p>
 *
 * @author Roman Puchkovskiy <a href="mailto:roman.puchkovskiy@blandware.com">
 *         &lt;roman.puchkovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.3 $ $Date: 2005/09/21 13:46:02 $
 * @see com.blandware.atleap.webapp.taglib.core.BaseHandlerTag
 * @see org.apache.struts.taglib.html.SubmitTag
 * @jsp.tag name="submit"
 * body-content="scriptless"
 */
public class SubmitTag extends BaseHandlerTag {
    /**
     * The name of the generated input field.
     */
    protected String property = null;

    /**
     * The value of the button label.
     */
    protected String value = null;

    /**
     * Whether to disable submit button after pressing it (to prevent multiple
     * submittion)
     */
    protected Boolean disableButtonOnSubmit = Boolean.FALSE;

    /**
     * Returns the property.
     *
     * @return property, which is the name of corresponding 'input' tag
     * @jsp.attribute required="false"
     * rtexprvalue="true"
     * type="java.lang.String"
     * description="Name of the 'input' tag"
     */
    public String getProperty() {

        return (this.property);

    }

    /**
     * Sets the property name.
     *
     * @param property The property name
     */
    public void setProperty(String property) {

        this.property = property;

    }

    /**
     * Returns the label value.
     *
     * @return value of the label
     * @jsp.attribute required="false"
     * rtexprvalue="true"
     * type="java.lang.String"
     * description="Label of the button"
     */
    public String getValue() {

        return (this.value);

    }

    /**
     * Sets the label value.
     *
     * @param value The label value
     */
    public void setValue(String value) {

        this.value = value;

    }

    /**
     * Gets a property specifiing whether to disable button after submit
     *
     * @return whether to disable button after submit
     * @jsp.attribute required="false"
     * rtexprvalue="true"
     * type="java.lang.Boolean"
     * description="Whether to disable button after submit"
     */
    public Boolean getDisableButtonOnSubmit() {
        return disableButtonOnSubmit;
    }

    /**
     * Sets whether to disable button after submit
     *
     * @param disableButtonOnSubmit whether to disable button after submit
     */
    public void setDisableButtonOnSubmit(Boolean disableButtonOnSubmit) {
        this.disableButtonOnSubmit = disableButtonOnSubmit;
    }

    /**
     * Processes the tag.
     *
     * @throws javax.servlet.jsp.JspException if a JSP exception has occurred
     * @throws java.io.IOException is an IO exception has occured
     */
    public void doTag() throws JspException, IOException {
        // Get text from inside of the tag
        String text;
        StringWriter writer = new StringWriter();
        JspFragment body = getJspBody();
        if (body != null) {
            body.invoke(writer);
            text = writer.toString().trim();
        } else {
            text = null;
        }

        // Acquire the label value we will be generating
        String label = value;
        if ((label == null) && (text != null)) {
            label = text;
        }
        if ((label == null) || (label.length() < 1)) {
            label = "Submit";
        }

        // Prepend disabling JS-code to onsubmit event handler, if needed
        String disablingCode = "this.disabled = true;";
        if (disableButtonOnSubmit == null) {
            disableButtonOnSubmit = Boolean.FALSE;
        }
        if (disableButtonOnSubmit.booleanValue()) {
            if (onclick == null) {
                onclick = disablingCode;
            } else {
                onclick = disablingCode + onclick;
            }
        }

        // Generate an HTML element
        StringBuffer results = new StringBuffer();
        results.append("<input type=\"submit\"");
        if (property != null) {
            results.append(" name=\"");
            results.append(property);
            results.append("\" ");
        }
        if (label != null) {
            results.append(" value=\"");
            results.append(label);
            results.append("\" ");
        }
        results.append(prepareAttributes());
        results.append("/>");

        // Write HTML-code to page
        PageContext pageContext = (PageContext) getJspContext();
        TagUtils.getInstance().write(pageContext, results.toString());
    }
}
